package com.yulinlin.admin.modules.sys.domain.config;


import com.yulinlin.admin.modules.sys.domain.SysElementEntity;
import com.yulinlin.admin.modules.sys.domain.SysMenuEntity;
import com.yulinlin.admin.modules.sys.domain.enums.SysMenuEnums;
import com.yulinlin.common.util.TreeUtil;
import com.yulinlin.data.core.model.BaseModelSelectWrapper;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class SysRoleConfig {

    //节点树
    private List<TreeNode> dataTree ;



    private void initTree(
            List<SysMenuEntity> sysMenuList,
            Map<String,List<SysElementEntity>> sysElementMap
    ){
        this.dataTree =  buildTree(sysMenuList,sysElementMap);
    }

    private List<TreeNode> buildTree(
            List<SysMenuEntity> sysMenuList,

            Map<String,List<SysElementEntity>> sysElementMap
    ){

        ArrayList list =   new ArrayList<>();

        for (SysMenuEntity sysMenuEntity : sysMenuList) {
            TreeNode parent = new TreeNode();
            list.add(parent);

            parent.setId(sysMenuEntity.getId());
            parent.setTitle(sysMenuEntity.getMenuName());
            List<TreeNode>  children = null;

            if(sysMenuEntity.getMenuType() == SysMenuEnums.MenuType.group){
                parent.setNodeType("group");
                children =  buildTree(sysMenuEntity.getChildren(),sysElementMap);

            }else if(sysMenuEntity.getMenuType() == SysMenuEnums.MenuType.menu){
                List<SysElementEntity> sysElementList =  sysElementMap.get(sysMenuEntity.getId());


                if(sysElementList  != null){
                    children =  sysElementList.stream().map(node -> {
                        TreeNode c = new TreeNode();
                        c.setId(node.getId());
                        c.setTitle(node.getTitle());
                        c.setNodeType("element");
                        return c;
                    }).collect(Collectors.toList());
                    parent.setChildren(children);
                }

            }
            parent.setChildren(children);


        }

        return list;


    }

  public static SysRoleConfig newInstance(){

        SysRoleConfig config =  new SysRoleConfig();

        List<SysMenuEntity> sysMenuEntityList   =  BaseModelSelectWrapper.newInstance(SysMenuEntity.class).selectList();
      sysMenuEntityList =  TreeUtil.buildTree(sysMenuEntityList);


        Map<String,List<SysElementEntity>> selectByGroup =  BaseModelSelectWrapper.newInstance(SysElementEntity.class).selectByGroup("sysMenuId");

      config.initTree(sysMenuEntityList,selectByGroup);
        return  config;
    }


    @Data
    public static class TreeNode{

        //id
        private String id;

        //标题
        private String title;

        //类型
        private String nodeType;

        private List<TreeNode> children;


        public void addChildren(TreeNode node){
            if(children == null){
                children = new ArrayList<>();
            }
            children.add(node);

        }


    }


}
